package com.glory.brepda.activity.mfg;

/**
 * 批次上架功能
 */

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.LotShelvesLotInfoAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.LotLists;
import com.glory.brepda.entity.LotactionLists;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.LotUtils;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


//{
//        "picName": "lot_shelves",
//        "showPlugin": "header",
//        "text": "批次上架(推荐)",
//        "skipClass": "com.glory.brepda.activity.mfg.LotUpSuggestActivity"
//        },

public class LotShelvesActivity extends BaseActivity {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etLotID)
    FreeUI_ClearEditText etLotID;
    @BindView(R.id.rvLotList)
    RecyclerView rvLotList;
    @BindView(R.id.etShelves)
    FreeUI_ClearEditText etShelves;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.llRbLayout)
    LinearLayout llRbLayout;
    @BindView(R.id.tvStepdesc)
    TextView tvStepdesc;
    @BindView(R.id.tvMainqty)
    TextView tvMainqty;
    private LotShelvesLotInfoAdapter mLotAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_lot_shelves;
    }

    @Override
    protected void initView() {
        initLayout();

        initEdit(etLotID, s -> {

            FreeUI_EntityCallBack<BaseResponseBean<LotInfoEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<LotInfoEntity>>
                    ("正在查询LOT信息", new TypeToken<BaseResponseBean<LotInfoEntity>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<LotInfoEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(LotShelvesActivity.this, true, getSupportFragmentManager())) {
                        tvStepdesc.setText(response.body().getResponse().getBody().getLOT().getSTEPDESC());
                        tvMainqty.setText(response.body().getResponse().getBody().getLOT().getMAINQTY());
                        etShelves.setText(response.body().getResponse().getBody().getLOCATORID());
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<LotInfoEntity>> response) {
                    super.onError(response);
                    loadError(response.getException(), "LOTQUERY");
                }
            };

            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE", "TESTBANKLOTLOCATORINFOBYID");
            map.addProperty("LOTID", s);
            NetUtils.requestPostNet("IF.LOTQUERY", "Common", LotShelvesActivity.this, map, callBack);
        });
    }

    private void initLayout() {
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        llRbLayout.setVisibility(View.GONE);
        setMainLayout(llMain);
        initRecyclerView(rvLotList,this);
        mLotAdapter = new LotShelvesLotInfoAdapter(null);
        mLotAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mLotAdapter.isFirstOnly(true);
        rvLotList.setAdapter(mLotAdapter);
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
//                if (actionControl("Submit10")) {
//                    return;
//                }

                if (getText(etLotID).equals("") || getText(etShelves).equals("")) {
                    showToast("批次号或货架号不能为空", false);
                    return;
                }

                showSubmitDialog(new onSubmitListener() {
                    @Override
                    public void setOnSubmitListener() {
                        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<JSONObject>>
                                (new TypeToken<BaseResponseBean<JSONObject>>() {
                                }.getType(), getSupportFragmentManager(), this) {

                            @Override
                            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                                super.onSuccess(response);
                                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                                    showToast("上架成功", true);
                                    etLotID.setText("");
                                    tvStepdesc.setText("");
                                    tvMainqty.setText("");
                                    etShelves.setText("");
                                }
                            }

                            @Override
                            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                                super.onError(response);
                                loadError(response.getException(),"CPCMOVELOCATOR");
                            }
                        };
                        JsonObject map = new JsonObject();
                        map.addProperty("ACTIONTYPE", "BIND");
                        map.addProperty("LOTID", getText(etLotID));
                        map.addProperty("LOCATORID", getText(etShelves));
//                        List<LotLists> lotList = new ArrayList<>();
//                        List<LotactionLists> lotactionList = new ArrayList<>();
//                        LotLists lotList1 = new LotLists(getText(etLotID));
//                        LotactionLists lotactionLists = new LotactionLists(getText(etLotID), "");
//                        lotList.add(lotList1);
//                        lotactionList.add(lotactionLists);
//                        map.add("LOTLIST", new JsonParser().parse(new Gson().toJson(lotList)).getAsJsonArray());//传批次集合
//                        map.add("LOTACTIONLIST", new JsonParser().parse(new Gson().toJson(lotactionList)).getAsJsonArray());
                        NetUtils.requestPostNet("IF.BREMOVELOCATOR", "Common", LotShelvesActivity.this, map, callBack);
                    }
                });
                break;
        }
    }

}
